package com.sudo.config;

import com.sudo.interceptor.AuthorizationInterceptor;
import com.sudo.interceptor.UserTokenInterceptor;
import com.sudo.pojo.properties.Some;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：为道日损 2020-04-24 17:40
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private Some some;

    /**
     *实现静态资源的映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                //映射swagger2 ,后面的/要带上
                .addResourceLocations("classpath:/META-INF/resources/")
                //映射本地静态资源 ,后面的/要带上
                .addResourceLocations("file:"+some.getFileSpace());
    }

    @Bean
    public UserTokenInterceptor userTokenInterceptor() {
        return new UserTokenInterceptor();
    }

    @Bean
    public AuthorizationInterceptor authorizationInterceptor() {
        return new AuthorizationInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor())
                .addPathPatterns("/sysUser/*")
                .excludePathPatterns(sysUserList())
                .addPathPatterns("/roleAuth/*")
                .excludePathPatterns(roleAuthList())
                .addPathPatterns("/sysRole/*")
                .addPathPatterns("/sysMenu/*")
                .excludePathPatterns(sysMenuList())
                .addPathPatterns("/userBehavior/*")
                .excludePathPatterns(userBehaviorList())
                .addPathPatterns("/sysButton/*")
                .addPathPatterns("/fileRun/*")
                .excludePathPatterns(fileRunList())
                .addPathPatterns("/sysDict/*")
                .addPathPatterns("/sysInfo/*");
        registry.addInterceptor(userTokenInterceptor())
                .addPathPatterns("/sysUser/*")
                .addPathPatterns("/sysMenu/*")
                .addPathPatterns("/sysRole/*")
                .addPathPatterns("/sysButton/*")
                .addPathPatterns("/userBehavior/*")
                .addPathPatterns("/fileRun/*")
                .addPathPatterns("/sysDict/*")
                .excludePathPatterns("/sysUser/userLogin",
                                "/sysUser/logout",
                                "/sysUser/isUserTokenRight");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    private List<String> sysUserList(){
        List<String> list = new ArrayList<>();
        list.add("/sysUser/userLogin");
        list.add("/sysUser/logout");
        list.add("/sysUser/getMyUserInfo");
        list.add("/sysUser/getMySimpleUserInfo");
        list.add("/sysUser/updateMyUserInfo");
        list.add("/sysUser/isUserTokenRight");
        return list;
    }

    private List<String> roleAuthList() {
        List<String> list = new ArrayList<>();
        list.add("/roleAuth/getRolePermission");
        list.add("/roleAuth/getMenuPermission");
        return list;
    }

    private List<String> sysMenuList() {
        List<String> list = new ArrayList<>();
        list.add("/sysMenu/queryMySysMenu");
        list.add("/sysMenu/getMenuPermission");
        return list;
    }

    private List<String> userBehaviorList() {
        List<String> list = new ArrayList<>();
        list.add("/userBehavior/queryMyBehaviorByPage");
        list.add("/userBehavior/queryMyLoginRecordByPage");
        return list;
    }

    private List<String> fileRunList() {
        List<String> list = new ArrayList<>();
        list.add("/fileRun/uploadMyLogo");
        list.add("/fileRun/getMyFilesByPage");
        return list;
    }
}
